﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace FLTools
{
    //xoml的名字； ""； 是否重要；是否紧急；意见；发送者角色；provider； email_url； orgkid； attachments
    public class FLInterfaceParam
    {
        private string _xomlFileName = string.Empty;
        private int _flowImportant = 0;
        private int _flowUrgent = 0;
        private string _remark = string.Empty;
        private string _providerName = string.Empty;
        private string _presentation = string.Empty;
        private string _orgKind = string.Empty;
        private string _attachments = string.Empty;
        private string _roleId = string.Empty;
        private object[] _clientInfo;

        public FLInterfaceParam(string xomlFileName, string providerName, string presentation, string roleId, object[] clientInfo)
        {
            _xomlFileName = xomlFileName;
            _providerName = providerName;
            _presentation = presentation;
            _roleId = roleId;
            _clientInfo = clientInfo;
        }

        public string XomlFileName
        {
            get
            {
                return _xomlFileName;
            }
            set
            {
                _xomlFileName = value;
            }
        }

        public int FlowImportant
        {
            get
            {
                return _flowImportant;
            }
            set
            {
                _flowImportant = value;
            }
        }

        public int FlowUrgent
        {
            get
            {
                return _flowUrgent;
            }
            set
            {
                _flowUrgent = value;
            }
        }

        public string Remark
        {
            get
            {
                return _remark;
            }
            set
            {
                _remark = value;
            }
        }

        public string ProviderName
        {
            get
            {
                return _providerName;
            }
            set
            {
                _providerName = value;
            }
        }

        public string Presentation
        {
            get
            {
                return _presentation;
            }
            set
            {
                _presentation = value;
            }
        }

        public string OrgKind
        {
            get
            {
                return _orgKind;
            }
            set
            {
                _orgKind = value;
            }
        }

        public string Attachments
        {
            get
            {
                return _attachments;
            }
            set
            {
                _attachments = value;
            }
        }

        public string RoleId
        {
            get
            {
                return _roleId;
            }
            set
            {
                _roleId = value;
            }
        }

        public object[] ClientInfo
        {
            get
            {
                return _clientInfo;
            }
            set
            {
                _clientInfo = value;
            }
        }

        // Order_Id = 'AAA123'，必须用单引号
        public object[] Submit()
        {
            if (string.IsNullOrEmpty(_xomlFileName) || string.IsNullOrEmpty(_providerName) || string.IsNullOrEmpty(_presentation)
                || string.IsNullOrEmpty(_roleId) || _clientInfo == null)
               return new object[] { 0, null };

            object[] p1 = new object[] { 
                _xomlFileName,
                "",
                _flowImportant,
                _flowUrgent, 
                _remark,
                _roleId,
                _providerName,
                0,
                _orgKind,
                _attachments
            };

            EEPRemoteModule remoteModule = new EEPRemoteModule();

            string[] ss = _providerName.Split(".".ToCharArray());
            if (ss.Length != 2)
                return new object[] { 0, null };

            object[] a = remoteModule.GetKeyFields(_clientInfo, ss[0], ss[1]);
            if(a[0].ToString() != "0")
                return new object[] { 0, null };

            string key = string.Empty;
            ArrayList list = (ArrayList)a[1];
            foreach (object item in list)
            {
                if (!string.IsNullOrEmpty(key))
                    key += ";";
                key += item.ToString();
            }
            
            string presentation = _presentation.Replace("'", "''");
            object[] p2 = new object[] {
                key,
                presentation
            };

            object[] p = new object[]{
                null, p1, p2
            };

            return remoteModule.CallFLMethod(_clientInfo, "Submit", p);
        }
    }
}
